package com.briup.smartcity_group1.web.controller.auth;

import com.briup.smartcity_group1.bean.basic.BaseUser;
import com.briup.smartcity_group1.consts.BeanNameConsts;
import com.briup.smartcity_group1.consts.DataMessageConsts;
import com.briup.smartcity_group1.service.IBaseUserService;
import com.briup.smartcity_group1.utils.result.Result;
import com.briup.smartcity_group1.web.vo.BaseUserUpdateVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/** 用户模块控制器
 * @author congee(congee02 @ 163.com)
 * @date 1/4/2023 9:47 PM
 */
@RestController
@RequestMapping(BeanNameConsts.BASE_USER)
public class BaseUserController {

    @Autowired
    private IBaseUserService service;

    @GetMapping(value = "info")
    public BaseUser currentUserInfo(@RequestParam(required = true) String token) {
        return service.currentUserInfo(token);
    }

    @GetMapping(value = "page")
    public PageInfo<BaseUser> findBaseUserLikeNameAndRoleByPage
                                (@RequestParam(required = false) String username, @RequestParam(required = false) Integer role, int pageNum, int pageSize) {
        return service.findBaseUserLikeNameAndRoleByPage(username, role, pageNum, pageSize);
    }

    @PutMapping
    public void updateById(BaseUserUpdateVO vo) {
        BaseUser toUpdate = new BaseUser();
        BeanUtils.copyProperties(vo, toUpdate);
        service.updateById(toUpdate);
        Result.setDataMessage(DataMessageConsts.USER_UPDATE_SUCCESS);
    }

    @PutMapping(value = "icon")
    public void changeIcon(Integer id,
                           String userIcon) {
        service.changeIcon(id, userIcon);
        Result.setDataMessage(DataMessageConsts.USER_CHANGE_ICON);
    }

    @DeleteMapping
    public void deleteById(int id) {
        service.deleteById(id);
        Result.setDataMessage(DataMessageConsts.USER_DELETE_SUCCESS);
    }

}
